<?php

/**
 * CommonAction
 * 
 * @package 
 * @author www.80boys.com
 * @copyright 2012
 * @version $Id$
 * @access public
 */
class CommonAction extends Action
{
    protected $admin = array(); // 当前请求类型
    protected static $acl = null;
    protected function _initialize()
    {
        Load('extend');
        //import("@.ORG.Page"); //导入分页类
        if(!D('User')->hasSignin())
        { // 检测用户是否登录
            $this->error('请登录', U('Public/login'));
        }
        $this->admin = session('_LOGIN_');
        Vendor('Zend.Acl');
        self::$acl = new Zend_Acl();
        $rolelist = D('User')->getRoleList();
        $reslist = D('User')->getResourceList();
        #创建角色
        foreach ($rolelist as $role)
        {
            self::$acl->addRole(new Zend_Acl_Role($role['id']));
        }
        #创建资源
        foreach ($reslist as $k => $v)
        {
            self::$acl->add(new Zend_Acl_Resource($v['name']));
        }
        #获取资源角色关联列表
        $rslist = M("resource_role")->query("select rr.*,r.name from __TABLE__   rr  INNER  JOIN " . C('DB_PREFIX') . "resource r  ON rr.resource_id = r.id ");
        foreach ($rslist as $k => $v)
        {
            self::$acl->allow($v['role_id'], $v['name']);
        }
    }
    #封装方法，数组也可
    protected function isAllowed($role = array(), $resource)
    {
        foreach ($role as $k => $v)
        {
            if(self::$acl->isAllowed($v['role_id'], $resource))
            {
                return true;
            }
        }
    }
}
